package com.example.grpc.client.dto;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import proto.com.example.grpc.common.UserProto;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class UserResponseDTO {
    @JsonProperty("user_id")
    private Integer userId;
    
    @JsonProperty("username")
    private String username;
    
    @JsonProperty("email")
    private String email;
    
    @JsonProperty("created_at")
    private Long createdAt;

    // 从 gRPC 对象转换的静态方法
    public static UserResponseDTO from(UserProto.UserResponse grpcResponse) {
        return new UserResponseDTO(
            grpcResponse.getUserId(),
            grpcResponse.getUsername(),
            grpcResponse.getEmail(),
            grpcResponse.getCreatedAt()
        );
    }

    // 转换为 gRPC 对象的方法
    public UserProto.UserResponse toGrpcUser() {
        return UserProto.UserResponse.newBuilder()
            .setUserId(this.userId != null ? this.userId : 0)
            .setUsername(this.username != null ? this.username : "")
            .setEmail(this.email != null ? this.email : "")
            .setCreatedAt(this.createdAt != null ? this.createdAt : 0L)
            .build();
    }
}